<?php
class my extends userBase {
    function indexTask(){
        $userId=$this->user->getLoginId();
         $commonmode=CommonMode::init();
        $r['userInfo']=UserMode::init()->getDetail($userId);
        $r['user']=UserChildModule::init("user")->getUserDetail($userId);
        
        $place_list=$r['user'];
        if($place_list['province_id']){
           $citylist= $commonmode->get_regions ( 2, $place_list ['province_id'] );
           $r['placecity_list']=$citylist;
        }
        if($place_list['city_id']){
            $districtlist= $commonmode->get_regions ( 3, $place_list ['city_id'] );
            $r['placedistrice_list']=$districtlist;
        }
        
        $r['hobbies']=!empty($r['userInfo']['hobbies'])?explode(',',$r['userInfo']['hobbies']):array();
        list($r['service'])=ServiceMode::init()->getList(1,100,0);
        $defaultAddress=AddressMode::init()->getDefault($userId);
        $r['defaultAddressId']=$defaultAddress['address_id'];
        $r['province_list']=$commonmode->get_regions(1,1);
        View::displayAsHtml($r, "my-info.php");
    }
    function myModifyTask($real_name,$idnum,$idnum_pic,$birth,$love,$email,$service_id,$province=null,$city=null,$district=null,$detailaddress=null){
       $province_id=(int)$province;
       $city_id=(int)$city;
       $district_id=(int)$district;
       $detailaddress=trim($detailaddress);
        $user_id=$this->user->getLoginId();
        $id=UserMode::init()->getIdByUserId($user_id);
        $data=array(
            'email'=>$email,
            'province_id'=>$province_id,
            'city_id'=>$city_id,
            'district_id'=>$district_id,
            'detailaddress'=>$detailaddress,
        );
        $modifyUser=UserChildModule::init("user")->modifyInfo_new($user_id,$data);
        
        
        $hobbies=!empty($love)? implode(',',$love):'';
        $data=array(
             'real_name'=>$real_name,
             'hobbies'=>$hobbies,
             'birth'=>$birth,
             'idnum'=>$idnum,
             'service_id'=>$service_id,
             'idnum_pic'=>$idnum_pic,
            );
        $modifyInfo=UserMode::init()->modify_new($id,$data);
        if($modifyInfo || $modifyUser){
            $result['message']="信息修改成功";
        }else{
            $result['message']="信息修改失败";
        }
        $result['url']=WebRouter::init()->getPage('my','info');
        View::displayAsHtml($result,"plugin/state/tips.php");
    }
    function passModifyTask($username,$pass,$newPass){
        if(empty($pass)||empty($newPass)){
            $result['message']="密码为空";
            View::displayAsHtml($result,"plugin/state/tips.php");
            exit();
        }
        $r=$this->user->modifyPass($username,$pass,$newPass);
        $result['message']=strtolower($r)=='success' ? "密码修改成功":$r;
        View::displayAsHtml($result,"plugin/state/tips.php");
    }
    function msgTask($page=1,$type){
        switch($type){
            case 'sys';$t='system';break;
            case 'shop';$t='shop';break;
            case 'rec';$t='recommend';break;
            case 'user';$t='user';break;
            default:$t='';
        }
        $userId=$this->user->getLoginId();
        $page= (int)((int)$page>0?$page:1);
        $countOfPage=10;
        $offset=($page-1)*$countOfPage;
        list($r['list'],$total)=MsgMode::init()->getList($userId,$t,$countOfPage,$offset);
        $url=WebRouter::init()->getQuestion("page=");
        $r['page']=new Page($page,$total,$countOfPage);
        $r['page']->setPageCallback(function($page)use($url,$type){
            return $url.$page."&type=".$type;
        });
        $r['pageEmpty']= $total==0 ? true:false;
        View::displayAsHtml($r, "my-msg.php");
    }
    // 我的账户
    function accountTask($page,$type){
        switch($type){
            case 'in';$t='in';break;
            case 'out';$t='out';break;
            case 'share';$t='share';break;
            case 'buy';$t='buy';break;
            case 'serviceFee';$t='serviceFee';break;
            case 'shopSell';$t='shopSell';break;
            default:$t='';
        }
        $page= (int)((int)$page>0?$page:1);
        $countOfPage=10;
        $offset=($page-1)*$countOfPage;
        $userId=$this->user->getLoginId();
        $userInfo=UserMode::init()->getDetail($userId);
        $r['money']=$userInfo['money'];
        list($r['list'],$total)=MoneyMode::init()->getList($userId,$t,$countOfPage,$offset);
        $url=WebRouter::init()->getQuestion("page=");
        $r['page']=new Page($page,$total,$countOfPage);
        $r['page']->setPageCallback(function($page)use($url,$type){
            return $url.$page."&type=".$type;
        });
        $r['type']=$type;
        $r['pageEmpty']=$total==0?true:false;
        View::displayAsHtml($r, "my-account.php");
    }
    function collectionTask($page){
        $userId=$this->user->getLoginId();
        $page= (int)((int)$page>0?$page:1);
        $countOfPage = 15;
        $offset=($page-1)*$countOfPage;
        list($r['list'],$total)=CollectModule::init('user_collect','user_id','gopen_id')
            ->getList($userId,$countOfPage,$offset);
        $ids=array();
        foreach ($r['list'] as &$v) {
            $ids[]=$v['gopen_id'];
        }
        $r['goodslist']=GoodsMode::init()->getSimpleInfo($ids);
        //我的collect_id呢。。。

        $url=WebRouter::init()->getQuestion("page=");
        $r['page']=new Page($page,$total,$countOfPage);
        $r['page']->setPageCallback(function($page)use($url){
            return $url.$page;
        });
        $r['pageEmpty']=$total==0?true:false;
        //var_dump($r);
        View::displayAsHtml($r, "my-collection.php");
    }
    function pointTask($page){
        $userId=$this->user->getLoginId();
        $r['user']=UserMode::init()->getDetail($userId);
        $r['nextLv']=UserMode::init()->getNextLv($r['user']['growth']);
        $r['coupon']=CouponMode::init()->getListByUser($userId);
        $page= (int)((int)$page>0?$page:1);
        $countOfPage = 5;
        $offset=($page-1)*$countOfPage;
        list($r['growth'],$total)=PointMode::init()->getList($userId,$countOfPage,$offset);
        $url=WebRouter::init()->getQuestion("page=");
        $r['page']=new Page($page,$total,$countOfPage);
        $r['page']->setPageCallback(function($page)use($url){
            return $url.$page;
        });
        $r['pageEmpty']=$total==0?true:false;
        View::displayAsHtml($r, "my-point.php");
    }
    // 报备进度查询
    function commodityTask($page){
        $page= (int)((int)$page>0?$page:1);
        $countOfPage = 10;
        $offset=($page-1)*$countOfPage;
        $userId=$this->user->getLoginId();
        list($r['list'],$total)=CommodityMode::init()->getList($userId,$countOfPage,$offset,'record');
        $url=WebRouter::init()->getQuestion("page=");
        $r['page']=new Page($page,$total,$countOfPage);
        $r['page']->setPageCallback(function($page)use($url){
            return $url.$page;
        });
        $r['pageEmpty']=$total==0?true:false;
        $r['commodityState']=array(
            'waiting'=>"已预约",'saw'=>"已看房",'deposit'=>"已缴纳定金",'front'=>"已交首付",
            'all'=>"已交全款",'finish'=>"完成销售",'profit'=>"已收到分成",
            'overtime'=>"报备已超时",'fake'=>"客服认定虚假报备");
        View::displayAsHtml($r, "my-commodity.php");
    }
    //TODO 服务站商家什么的申请过一次就不能再申请的说
    function serviceTask(){
        $r=array();
        View::displayAsHtml($r,"apply-station.php");
    }
    function serviceSubmitTask($real_name,$sex ='male',$birth,$academic,$idnum, $idnum_pic,
                               $header_name,$header_tel,$service_name,$facade_pic,$nation,$tel,
                               $email, $address,$resume,$computer_skill,$address_id,$address_text,
                                $latitude,$longitude){
        $userId=$this->user->getLoginId();
        $s=ServiceMode::init();
        if($s->checkExist($userId)){
            $result['message']="您已经申请过服务站了，有任何问题请联系客服";
        }else{
            $returnId=$s->add($userId,$real_name,$sex,$birth,$academic,$idnum,
                $idnum_pic,$header_name,$header_tel,$service_name,$facade_pic,$nation,$tel,$email,
                $address,$resume,$computer_skill,$address_id,$address_text,$latitude,$longitude);
            if($returnId!=-1){
                //申请成功
                $result['message']="申请成功，请耐心等待，申请成功我们会在第一时间告知，祝我们合作愉快~";
            }else{
                $result['message']="申请失败，请检查一下您的网络哦";
            }
        }
        $result['url']=WebRouter::init()->getPage('home','index');
        View::displayAsHtml($result,"plugin/state/tips.php");
    }
    function shopTask(){
        $r=array();
        View::displayAsHtml($r,"apply-shop.php");
    }
    function shopSubmitTask($real_name,$sex='male',$tel, $academic,$birth, $idnum,
                            $idnum_pic, $company, $license_pic, $company_pic, $nation,$qq, $email,
                            $address, $computer_skill, $recent_pic, $introduction, $address_id,
                            $address_text, $visible, $latitude,$longitude){
        if(empty($birth)){
            $result['message']="请检查生日日期是否有效";
        }
        $userId=$this->user->getLoginId();
        $s=ShopMode::init();
        if($s->checkExist($userId)){
            $result['message']="您已经申请过商家了，有任何问题请联系客服";
        }else{
            $returnId=$s->add($userId,$real_name,$sex,$tel, $academic,$birth, $idnum,
                $idnum_pic, $company, $license_pic, $company_pic, $nation,$qq, $email,
                $address, $computer_skill, $recent_pic, $introduction, $address_id,
                $address_text, $visible, $latitude,$longitude);
            if($returnId!=-1){
                //申请成功
                $result['message']="申请成功，请耐心等待，申请成功我们会在第一时间告知，祝我们合作愉快~";
            }else{
                $result['message']="申请失败，请检查一下您的网络哦";
            }
        }
        $result['url']=WebRouter::init()->getPage('home','index');
        View::displayAsHtml($result,"plugin/state/tips.php");
    }
    //用户设置默认服务站
    function setDefaultServiceTask($service_id){
        $user_id=$this->user->getLoginId();
        $id=UserMode::init()->getIdByUserId($user_id);
        $userInfo=UserMode::init()->getDetail($id);
        if(!empty($service_id) && $userInfo['service_id']==$service_id){
            $result['message']="该服务站已经是默认服务站";
        }else{
            $r=UserMode::init()->setDefaultService($id,$service_id);
            $result['message']=$r? "设置成功":"设置失败";
        }
        View::displayAsHtml($result,"plugin/state/tips.php");
    }
    function recordTask($gopen_id,$name,$tel){
        $c=CommodityMode::init();
        $id=$this->user->getLoginId();
        $userInfo=$this->user->getDetail($id);
        if($userInfo['username']==$tel){
            $result['message']="不能写您自己的手机号哦";
        }else{
            $check=$c->checkTelValid($tel);
            if(strtolower($check)!='success'){
                $result['message']=$check;
            }else{
                //可以报备
                $r=$c->addRecord($gopen_id,$id,$name,$tel,'record');
                if($r!=-1){
                    $result['message']="报备成功";
                }else{
                    $result['message']="报备失败，请重试";
                }
            }
        }
        View::displayAsHtml($result,"plugin/state/tips.php");
    }
    //不能重复团购
    function groupTask($gopen_id,$name,$tel){
        $c=CommodityMode::init();
        $id=$this->user->getLoginId();
        $check=$c->checkGroupTelValid($tel);
        if(strtolower($check)!='success'){
            $result['message']=$check;
        }else{
            //可以团购
            $r=CommodityMode::init()->addRecord($gopen_id,$id,$name,$tel,'group');
            if($r!=-1){
                $result['message']="团购成功";
            }else{
                $result['message']="团购失败，请检查该手机号是否已经团购过了";
            }
        }
        View::displayAsHtml($result,"plugin/state/tips.php");
    }

    function browseTask($page){
        $id=$this->user->getLoginId();
        $page= (int)((int)$page>0?$page:1);
        $countOfPage=16;
        $offset=($page-1)*$countOfPage;
        list($r['goods'],$total)=UserMode::init()->getBrowseList($id,$countOfPage,$offset);
        $a=array_map(function($v){ return $v['gopen_id'];},$r['goods']);
        $result['list']=GoodsMode::init()->getSimpleInfo($a);
        $url=WebRouter::init()->getQuestion("page=");
        $result['page']=new Page($page,$total,$countOfPage);
        $result['page']->setPageCallback(function($page)use($url){
            return $url.$page;
        });
        $result['pageEmpty']=$total==0?true:false;
        //加上browser 的id
        $result['browser'] = $r['goods'];
        View::displayAsHtml($result,"my_browse.php");
    }
    function deleteBrowseTask(){
        $UserId=$this->user->getLoginId();
        $r=UserMode::init()->deleteBrowse(null,$UserId);
        $result['message']=$r?"所有浏览历史删除成功":"删除失败";
        View::displayAsHtml($result,"plugin/state/tips.php");
    }
    //催发货
    function sendShopMsgTask($user_id,$order_id){
        $o = OrderMode::init();
        $userId=$this->user->getLoginId();
        $userInfo=UserModule::init('user')->getLoginInfo();
        $valid=$o->checkOrderBelongsToUser($order_id,$userId);
        if(empty($order_id)||empty($user_id)||!$valid){
            echo "非法操作";
            return null;
        }

        $title='客户'.$userInfo['username'].'催促发货';
        $goodsDetail=$o->getGoodsDetail($order_id);
        $goods='';
        if(!empty($goodsDetail)){
            foreach ($goodsDetail as $v) {
                $name=$v['name'];
                $number=$v['number'];
                $goods.=$name.'*'.$number.' ';
            }
        }
        $content="客户".$userInfo['username']."提醒您将订单号为".$order_id."的产品尽快发货，商品名 ".$goods."。";
        $type='user';
        $userId=ShopMode::init()->getUserId($user_id);
        $r=MsgMode::init()->send($userId,$title,$content,$type);
        $result['message']=$r?"已经提醒卖家发货了":"提醒失败，请重试";
        View::displayAsHtml($result,"plugin/state/tips.php");
    }
    //催配送
    function sendServiceMsgTask($user_id,$order_id){
        $o = OrderMode::init();
        $userId=$this->user->getLoginId();
        $userInfo=UserModule::init('user')->getLoginInfo();
        $valid=$o->checkOrderBelongsToUser($order_id,$userId);
        if(empty($order_id)||empty($user_id)||!$valid){
            echo "非法操作";
            return null;
        }
        $title='客户'.$userInfo['username'].'催促配送';
        $goodsDetail=$o->getGoodsDetail($order_id);
        $goods='';
        if(!empty($goodsDetail)){
            foreach ($goodsDetail as $v) {
                $name=$v['name'];
                $number=$v['number'];
                $goods.=$name.'*'.$number.' ';
            }
        }
        $content="客户".$userInfo['username']."提醒您将订单号为".$order_id."的产品尽快配送，商品名 ".$goods."。";
        $type='user';
        $userId=ServiceMode::init()->getUserId($user_id);
        $r=MsgMode::init()->send($userId,$title,$content,$type);
        $result['message']=$r?"已经提醒服务站配送了":"提醒失败，请重试";
        View::displayAsHtml($result,"plugin/state/tips.php");
    }
    
    function ajaxurlTask($type,$parent){
       $parent=(int)$parent;
       $type=(int)$type;
        $commonmode=CommonMode::init();
        $list=$commonmode->get_regions($type,$parent);
        $str="";
        $str.=" <option value='0'>请选择</option>";
        foreach($list as $k=>$v){
            $str.="<option value='".$v['region_id']."'> ".$v['region_name']."</option>";
        }
        die(json_encode($str));
    }
}